package com.yager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yager.mapper.HouseMapper;
import com.yager.model.House;
import com.yager.model.Page;
import com.yager.service.HouseService;
import com.yager.util.NameUtil;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Component
@NoArgsConstructor
public class HouseServiceImpl implements HouseService {


    @Autowired
    private HouseMapper houseMapper;

    @Override
    public List<House> selectHouseByConditions(String housetype, String houseaddress, boolean sortage, boolean sortprice) {
       List<House> houseList = houseMapper.selectHouseByConditions(housetype, houseaddress, sortage, sortprice);
        return houseList;
    }

    @Override
    public House selectHouseByPrimaryKey(String houseid) {
        House house = houseMapper.selectByPrimaryKey(houseid);
        return house;
    }


    @Override
    @Transactional
    public void insertHouse(MultipartFile[] multipartFile, House house) {
        String max=houseMapper.findMaxId();
        max=max.substring(1);
        Integer i=Integer.parseInt(max);
        i++;
        String id="H";
        if(i<10)
            id=id+"00"+i.toString();
        else if(i<100)
            id=id+"0"+i.toString();
        else
            id=id+i.toString();
        house.setHouseid(id);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
        String dataPath = "house" + "/" + simpleDateFormat.format(new Date()) + "/" + id;
        System.out.println(dataPath);
        house.setHouseimages(dataPath);
        /*存入HouseImage的名称*/
        List<File> fileList = new ArrayList<File>();
        for(int j = 0; j < multipartFile.length; j++){
            String oldImgName = multipartFile[j].getOriginalFilename();
            String imgSuffix = oldImgName.substring(oldImgName.lastIndexOf("."));
            NameUtil nameUtil = new NameUtil();
            String imgName = nameUtil.GeneratorhouseImgName() + imgSuffix;
            File targetfile = new File("D://img/" + dataPath);
            if(!targetfile.exists()) targetfile.mkdirs();
            /*指定图片名称*/
            File trueTargetFile = new File(targetfile, imgName);
            try {
                multipartFile[j].transferTo(trueTargetFile);
            }catch (IOException e){
                e.printStackTrace();
            }
        }
        houseMapper.insertSelective(house);
    }

    @Override
    public void deleteHouse(String houseid) {
        log.info("删除进行中");
        houseMapper.deleteByPrimaryKey(houseid);
        houseMapper.removeCollectionByHouseId(houseid);
        houseMapper.removeOrderByHouseId(houseid);
        log.info("删除成功");

    }

    @Override
    public void updateHouse(House house) {
        log.info("更新进行中");
        houseMapper.updateByPrimaryKeySelective(house);
        log.info("更新完成");
    }

    @Override
    public PageInfo<House> selectHouseByText(String housetext, Integer pageNo) {
        PageHelper.startPage(pageNo, 4);
        List<House> houseList = houseMapper.selectHouseByText(housetext);
        PageInfo<House> pageInfo = new PageInfo<>(houseList, 5);
        return pageInfo;
    }

    @Override
    public List<House> selectHouseByTextAndConditions(String housetext, String housetype, String houseaddress, int sortage, int sortprice) {
        List<House> houseList = houseMapper.selectHouseByTextAndConditions(housetext, housetype, houseaddress, sortage, sortprice);
        return houseList;
    }

    @Override
    public PageInfo<House> selectHouseOrderById(Integer pageNo) {
        PageHelper.startPage(pageNo, 4);
        List<House> houseList = houseMapper.selectHouseOrderById();
        PageInfo<House> pageInfo = new PageInfo<>(houseList, 5);
        return pageInfo;
    }

    @Override
    public PageInfo<House> selectHouseByUserId(String userid, Integer pageNo) {
        PageHelper.startPage(pageNo, 4);
        List<House> houseList = houseMapper.selectHouseByUserId(userid);
        PageInfo<House> pageInfo = new PageInfo<>(houseList, 5);
        return pageInfo;
    }

    @Override
    public String getHouseNameById(String houseid){
        String housename=houseMapper.selectHouseNameById(houseid);
        return housename;
    }
}
